package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmeaIDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DishServiceimpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmeaIDishMapper setmeaIDishMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //向菜品表插入数据
         dishMapper.insert(dish);
         long id = dish.getId();
        //想口味表插入数据
        List<DishFlavor> flavors  = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {flavor.setDishId(id);});
            dishFlavorMapper.insertBath(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> dishId) {
        //判断当前商品是否为预售
        dishId.forEach(id -> {
            Dish dish = dishMapper.getById(id);
           if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        });
        //判断菜品是否与套餐关联
        List<Long> setmealIds = setmeaIDishMapper.getSetmeaIDishId(dishId);
        if (setmealIds != null && !setmealIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表的菜品数据

            dishMapper.deleteById(dishId);

        //删除菜品关联的口味数据
           dishFlavorMapper.deleteByDishId(dishId);


    }

    @Override
    public DishVO getByIdWithFlavor( Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.getById(id);
        //根据菜品id查询各种口味数据
        List<DishFlavor> dishFlavors =   dishFlavorMapper.getByDishid(id);
        //将查询到的数据进行封装
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updeteWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        List<Long> id = new ArrayList<>();
        id.add(dish.getId());
        dishFlavorMapper.deleteByDishId(id);
        List<DishFlavor> dtoFlavors = dishDTO.getFlavors();
        if (dtoFlavors != null && !dtoFlavors.isEmpty()) {
            dtoFlavors.forEach(flavor -> {flavor.setDishId(dish.getId());});
        }
        dishFlavorMapper.insertBath(dtoFlavors);

    }
}
